const app = getApp();
import {
  post
} from '../../utils/request';
import {
  getPlaceInfo
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: null,
    reasonList: [],

    remark: undefined,
    reasonIndex: -1,
  },

  // 确定退款
  onRefund() {
    if (this.data.reasonIndex < 0) {
      wx.showToast({
        title: '请选择退款理由',
        icon: 'error'
      });
      return;
    }
    if (this.data.reasonList[this.data.reasonIndex] === '其他原因') {
      if (!this.data.remark) {
        wx.showToast({
          title: '请填写退款备注',
          icon: 'error'
        });
        return;
      }
    }
    post('/order/refund', {
      orderNo: this.data.detail.orderNo,
      refundReason: this.data.reasonList[this.data.reasonIndex],
      refundRemark: this.data.remark
    }).then(res => {
      if (res.code === 0) {
        wx.showModal({
          title: '提示',
          content: '退款提交成功，我们将及时跟进！',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/order/my',
              });
            }
          }
        })
      }
      if (res.code === -1) {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        });
      }
    });
  },

  // 获取订单详情
  getDetail() {
    let that = this;
    let {
      orderNo
    } = app.getPageOptions();
    if (!orderNo) {
      wx.showModal({
        title: '提示',
        content: '未找到订单编号',
      });
    }
    app.checkLogin(true, () => {
      post(`/order/detail/${orderNo}`).then(res => {
        if (res.code === 0) {
          let {
            data
          } = res;
          that.setData({
            detail: data,
          });
        }
        if (res.code === -1) {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          });
        }
      });
    });
  },

  bindPickerChg(e) {
    this.setData({
      reasonIndex: e.detail.value
    })
  },

  onRemarkChg(e) {
    let {
      value
    } = e.detail;
    this.setData({
      remark: value
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setBackgroundColor({
      backgroundColor: "#f6f6f6"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDetail();

    post(`/common/places/SVIP_REFUND_REASON`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            reasonList: getPlaceInfo(resp.data, 'SVIP_REFUND_REASON').cmsPlaceInfoList.map(d => d.title)
          });
        }
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
});